<?php

namespace app\api\model;

use app\common\model\RemindCourse as RemindCourseModel;

class RemindCourse extends RemindCourseModel
{
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';



    /**
     * 新增
     */
    public function add(array $data)
    {
        return $this->allowField(true)->save($data);
    }

    /**
     * 课次
     */
    public static function getByCourseId($courseId, $type)
    {
        return self::where('course_id', $courseId)->where('type', $type)->find();
    }

    /**
     * 单条信息
     */
    public static function getByUserId($userId)
    {
        return self::where('user_id', $userId)->order('createtime desc')->whereIn('type', [1,2])->find();
    }

    /**
     * 是否有未读
     */
    public static function getReadingByUserId($userId)
    {
        return self::where('user_id', $userId)->where('reading', 0)->whereIn('type', [1,2])->count();
    }

    /**
     * 用户课程提醒
     */
    public static function getAllByUserId($userId, $size = 20)
    {
        return self::with(['courseOrder', 'teacher' => function ($query) {
            $query->withCount('course');
        }])->where('user_id', $userId)->whereIn('type', [1,2])->order('createtime desc')->paginate($size);
    }



}
